package jdom;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import java.io.IOException;
import java.util.List;

public class TestJdomParser {

    public static void main(String[] args) {

        long start=System.currentTimeMillis();

        //创建读取xml的工具类
        SAXBuilder builder=new SAXBuilder();

        //读取xml文件，并解析，变成一个树（jdom自己做的树）
        try {
            Document document=builder.build(TestJdomParser.class
                                                          .getClassLoader()
                                                          .getResourceAsStream("students.xml"));


            Element classElement=document.getRootElement();

            System.out.println(classElement.getName());

            String no=classElement.getAttributeValue("no");

            System.out.println(no);

            List<Element> studentElements=classElement.getChildren("student");


            for (Element studentElement : studentElements) {
                List<Attribute> attributes=studentElement.getAttributes();

                for (Attribute attribute : attributes) {
                    System.out.println(attribute.getName()+"="+attribute.getValue());
                }


                // Element nameElement=studentElement.getChild("name");
                //
                // System.out.println(nameElement.getText());

                System.out.println(studentElement.getChildText("name"));


            }








        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }



        long end=System.currentTimeMillis();

        System.out.println(end-start);
    }


}
